import matplotlib.pyplot as plt
from shutil import copyfile
from sys import exit
from pathlib import Path
import numpy as np
import time

def ParseData( fileName ):
    # 打开一个文件
    f = open(fileName, "r+")
    lines = f.readlines()             # 调用文件的 readline()方法
    f.close()
    
    puOutX = []
    puOutY = []
    
    pdOutX = []
    pdOutY = []
    
    tbOutX = []
    tbOutY = []
    
#     while lines:5
    for line in lines:
        #print(lines)
        strs = line.split(";")
#         if (strs[0] != "IN"):
#             continue;
        isPu = False
        pdOutXSub = []
        pdOutYSub = []
        for pointStr in strs:
            #print(pointStr)
            if( pointStr.startswith("PU") ):
                pointData = pointStr[2:].split(",")
                puOutX.append( int(pointData[0]) )
                puOutY.append( int(pointData[1]) )
                isPu = True
                
            elif( pointStr.startswith("PD") ):                
                if ( isPu ):
                    isPu = False
                    if(pdOutXSub):
                        pdOutX.append( pdOutXSub ) 
                        pdOutY.append( pdOutYSub )
                        
                        #print("图形:", pdOutXSub, pdOutYSub )
                        pdOutXSub = []
                        pdOutYSub = []   
                
                pointData = pointStr[2:].split(",")
                pdOutXSub.append( int(pointData[0]) ) 
                pdOutYSub.append( int(pointData[1]) )
                
            elif( pointStr.startswith("TB") ):
                pointData = pointStr[2:].split(",")
                tbOutX.append( int(pointData[1]) ) 
                tbOutY.append( int(pointData[2]) )
     
        if(pdOutXSub):
            pdOutX.append( pdOutXSub ) 
            pdOutY.append( pdOutYSub )
                                    
            pdOutXSub = []
            pdOutYSub = []
                        
    return puOutX, puOutY, pdOutX, pdOutY, tbOutX, tbOutY
    
#a=[1,12,3,14,5,16,7,16,5,14,3,12,1]
#a_x=[11,12,13,14,15,16,17,18,19,20,21,22,23]

puOutX, puOutY, pdOutX, pdOutY, tbOutX, tbOutY = ParseData( "Z:/Temp/OutData.fil" )

rng = np.random.RandomState(10)
color = rng.rand(len(pdOutX))
#plt.plot(a) 
#plt.plot( pdOutX, pdOutY, 'ro')
plt.xticks = 500
plt.yticks = 500

ax = plt.gca()
#ax.xaxis.set_ticks_position('top')  #将y轴的位置设置在右边
#ax.yaxis.set_ticks_position('left')  #将y轴的位置设置在右边
#ax.invert_yaxis()  #y轴反向

plt.scatter(puOutX, puOutY, marker='+')
plt.scatter(tbOutX, tbOutY, s=20, marker='>')
#plt.scatter(pdOutX, pdOutY, c=color, s=5, marker='>')
#print(pdOutX, pdOutY )
print("pdOutX", len(pdOutY), len(range(0, 256)))

for ind in range(0, len(pdOutX)):
#    print(ind, pdOutX[ind], pdOutY[ind] )
    plt.scatter(pdOutX[ind], pdOutY[ind], marker='+')
    #plt.plot(pdOutX[ind], pdOutY[ind])
    
#axx = plt.axes()
for ind in range(1, len(puOutX)-1):
    plt.arrow(puOutX[ind-1], puOutY[ind-1], puOutX[ind]-puOutX[ind-1], puOutY[ind]-puOutY[ind-1], head_width=100, head_length=200, length_includes_head = True, linestyle='-.')   

#sortedX = sorted(pdOutX)
#sortedY = sorted(pdOutY)

#print( sortedX, sortedY )
#disX = abs( sortedX[0] - sortedX[-1] )
#disY = abs( sortedY[0] - sortedY[-1] )

#centorX = (sortedX[0]+sortedX[-1])/2
#centorY = (sortedY[0]+sortedY[-1])/2

#centorStr = "(W:" + str(disX) + ",H:" + str(disY) + "\n minX:" + str(sortedX[0]) + ", maxX:" + str(sortedX[-1]) + "\n minY:" + str(sortedY[0]) + ", maxY:" + str(sortedY[-1]) + ")"

#plt.annotate( s=centorStr, xy=(centorX, centorY) )

# axisOffset = np.random.randint(-500, 500, size=len(pdOutX)*2)
# for ind in range(0, len(pdOutX)):    
#     labelStr = str(ind)+":" +str(pdOutX[ind]) +","+str(pdOutY[ind])
#     plt.annotate( s=labelStr, xy=(pdOutX[ind], pdOutY[ind]),
#                   xytext=( pdOutX[ind]+axisOffset[ind], pdOutY[ind]-axisOffset[len(pdOutX)-ind] ),
#                   arrowprops=dict(arrowstyle='-'))

time_tup = time.localtime(time.time())

format_time='%Y%m%d%a_%H%M%S'
cur_time = time.strftime(format_time, time_tup)
plt.savefig('Z:/'+cur_time+'.svg')

plt.show()
